package com.lzw.cpvideoplayer.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lzw.cpvideoplayer.R;
import com.lzw.cpvideoplayer.data.cHeadData;
import com.lzw.cpvideoplayer.ulit.OnItemClickLiner;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.MyHolder> {
    private static final String TAG = "ContentAdapter";
    public static int currPosition;
    private Context mContext;
    private cHeadData data;
    private OnItemClickLiner itemClickLiner;

    public ContentAdapter(Context mContext, cHeadData data) {
        this.mContext = mContext;
        this.data = data;
        currPosition = 0;

    }

    public void setOnItemListener(OnItemClickLiner itemListener){
        itemClickLiner = itemListener;
        Log.e("MainListHolder", "onClick: "+itemClickLiner );
    }

    @Override
    public int getItemViewType(int position) {

        return super.getItemViewType(position);
    }

    @NonNull
    @Override
    public ContentAdapter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyHolder(LayoutInflater.from(mContext).inflate(R.layout.main_list_item,parent,false),itemClickLiner);
    }

    @Override
    public void onBindViewHolder(@NonNull ContentAdapter.MyHolder holder, int position) {
       if(currPosition == position){
           holder.mTitle.setTextColor(Color.RED);

       }else {
           holder.mTitle.setTextColor(Color.BLACK);
       }
        holder.mTitle.setText(data.getData().getChapterList()[position].getTitle());
    }

    @Override
    public int getItemCount() {
        return data.getData().getChapterList().length;
    }

    public class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView mTitle;
        private OnItemClickLiner onliner;

        public MyHolder(@NonNull View itemView, OnItemClickLiner onitemclick) {
            super(itemView);
            itemView.setOnClickListener(this);
            mTitle = itemView.findViewById(R.id.list_text);
            onliner = onitemclick;
        }

        @Override
        public void onClick(View v) {

            onliner.OnItemClick(v,getAdapterPosition());
        }
    }
}
